<?php
	Class BlockedItem extends UsersAppModel
	{
		var $name = 'BlockedItem';
		
		function findBlockedItems($type,$userId)
		{
			if($list = gCache($this->getCacheKey($type,$userId)))
				return unserialize($list);
			$list = $this->find('list',array('conditions'=>array('type'=>$type,'user_id'=>$userId),'fields'=>array('id','item_id')));
			$list = array_unique($list);
			gCache($this->getCacheKey($type,$userId),serialize($list),'+10 minutes');
			return $list;
		}
		
		private function getCacheKey($alias,$userId)
		{
			return 'user_content_blocked_'.low($alias).'_'.$userId;
		}
		
		/**
		 * Save Blocked Item For particuakr user
		 * @param $Type
		 * @param $itemId
		 * @param $userId
		 * @return unknown_type
		 */
		function blockItems($type,$itemId = array(),$userId = null)
		{
			if(is_numeric($itemId))
				$itemId = (array)$itemId;
			foreach($itemId as $item)
			{
				$data['BlockedItem'] = array('item_id'=>$item,'type'=>$type,'user_id'=>$userId);
				$this->create();
				$this->save($data);	
			}
			Cache::delete(cr('appCode').'_'.$this->getCacheKey($type,$userId));
		}
		/**
		 * Delete Items from blockItems
		 * @param $type
		 * @param $userId
		 * @param $itemId
		 * @return unknown_type
		 */
		function removeBlockedItem($type,$itemId = array(),$userId=null)
		{
			if(is_numeric($itemId))
				$itemId = (array)$itemId;
			$this->deleteAll(array('user_id'=>$userId,'type'=>$type,'item_id'=>$itemId));
			Cache::delete(cr('appCode').'_'.$this->getCacheKey($type,$userId));
		}
		
		function isBlocked($type,$itemId,$userId)
		{
			return in_array($itemId,$this->findBlockedItems($type,$userId));
		}
		
	}
?>